<?php

class helpModel extends Model 
{
    public static function validata($data) {
        return Validation::rule(array(
            [Validation::isMust($data['title']), '标题不能为空'],
            [Validation::isMust($data['image']), '封面图片不能为空'],
            [Validation::isLength($data['title'], 1, 150), '标题长度在150字符以内']
        ));
    }
    
    public function findById($id) {
        if(!$info = $this->getOne('id=?', $id)) {
            return false;
        }
        return [
            'id' => intval($info['id']),
            'title' => strval($info['title']),
            'content' => $info['content'],
            'addTime' => $info['add_time'],
            'updateTime' => $info['update_time']
        ];
    }
    
    public function getList($where, $value, $order, $page, $pageSize) {
        $limit = ($page-1)*$pageSize;
        $offset = $pageSize;
        $total = $this->getCount($where, $value);
        $resultData = [
            'page' => intval($page),
            'pageSize' => intval($pageSize),
            'total' => intval($total),
            'list' => []
        ];
        if($total>$limit) {
            $list = $this->order($order)->limit($limit, $offset)->getAll($where, $value);
            if($list) {
                foreach ($list as $row) {
                    $resultData['list'][] = [
                        'id' => intval($row['id']),
                        'title' => strval($row['title']),
                        'addTime' => $row['add_time']
                    ];
                }
            }
        }
        return $resultData;
    }
    
    public function getCount($where, $value) {
        return $this->count(null, $where, $value);
    }
}